<?php
echo $this->headScript()->setFile($this->baseUrl().'/js/jquery/table-sorter/jquery.dataTables.js');

function comment_truncate($phrase,$start_words, $max_words)
{
   $phrase_array = explode(' ',$phrase);
   if(count($phrase_array) > $max_words && $max_words > 0)
	  $phrase = implode(' ',array_slice($phrase_array, $start_words, $max_words)).'...';  
   return $phrase;
}
?>
<script type="text/javascript">
$(document).ready(function() {
	floatingbar('#floatingbar',"#button_top_bar",null);
	/****************************Sorting Table Start*********************/
	var oTable = $("#sortTable").dataTable({
				"bPaginate": false,
				"bLengthChange": true,
				"bSearchable": true,
				"bFilter": true,
				"bSort": true,
				"bInfo": false,
				"bAutoWidth": true,
				"bProcessing": false,
				"bSortClasses": true,
				"bStateSave": false,
				"bServerSide": false,	
				"aaSorting": [[ 1, "asc" ]],			
				"aoColumnDefs": [ 
						{ "bSortable": false, "bSortClasses": false,  "aTargets": [ 0 ] }
					],
				"oLanguage" : { "sSearch": "<?php echo $this->translator->translator('jquery_filter'); ?>" }
			}); 	
	
	$('td', oTable.fnGetNodes()).hover( function() {		
		$(this).parents("tr:first").children('td').addClass( 'highlighted' );
	}, function() {
		$('td.highlighted').removeClass('highlighted');
	} );
	
	/* Get the rows which are currently selected */
	function fnGetSelected( oTableLocal )
	{
		var aReturn = new Array();
		var aTrs = oTableLocal.fnGetNodes();
		
		for ( var i=0 ; i<aTrs.length ; i++ )
		{
			if ( $(aTrs[i]).hasClass('row_selected') )
			{
				aReturn.push( aTrs[i] );
			}
		}
		return aReturn;
	}
	
	lastClassAdd(oTable);
	
	/****************************Sorting Table End*********************/
	
		function openMsgDialog(msg)
		{
			$('#dialog_container').html(msg).dialog({
						autoOpen: true,
						modal: false,
						show: 'fade',
						resizable: 'false',				
						buttons: { "<?php echo html_entity_decode($this->translator->translator('common_ok'), ENT_QUOTES, "UTF-8"); ?>": function() { $(this).dialog('option', 'hide', 'explode').dialog("close"); } },
						open: function(event, ui)
							 {
								setTimeout(function() 
								{									
									if($('#' + event.target.id).dialog('isOpen'))
									{
										$('#' + event.target.id).dialog('option', 'hide', 'explode').dialog('close');										
									}
								}, 10000);
							  }
					});
		}
		
	$('a.publish_btn').click(function() {
		var self = this;
		
		<?php if ($this->allow('publish','backendsetting','Comment')) { ?>		
		var arr = $(self).attr('rel').split('_');		
		$.ajax({
			 url: "<?php echo $this->url( array('module'=> 'Comment', 'controller' => 'backendsetting', 'action'     => 'publish' ), 'adminrout',    true) ?>",
			 type: 'POST',
			 data: { id: arr[0], paction: arr[1]},
			 beforeSend: function(){
						$(self).html('<img src="application/modules/Administrator/layouts/scripts/images/loader/ajax_loader.gif" border="0" height="17" />');
					 },
			 success: function(response) 
			 {
			 	//alert(response);
			 	var json_arr = eval("("+response+")");
				if(json_arr.status == 'ok')
				{
					if(json_arr.active == '1')
					{
						$(self).html('<img src="application/modules/Administrator/layouts/scripts/images/tools/published.gif" border="0" title="<?php echo $this->translator->translator("comment_setting_off_language_title"); ?>" alt="<?php echo $this->translator->translator("comment_setting_off_language_title"); ?>" />').attr('rel',arr[0]+"_unpublish");
					}
					else
					{
						$(self).html('<img src="application/modules/Administrator/layouts/scripts/images/tools/unpublished.gif" border="0" title="<?php echo $this->translator->translator("comment_setting_on_language_title"); ?>" alt="<?php echo $this->translator->translator("comment_setting_on_language_title"); ?>" />').attr('rel',arr[0]+"_publish");
					}
			 	}
				else
				{
					openMsgDialog(json_arr.msg);
				}
			 }
		});
		<?php }else{ ?>
			var msg = "<?php echo $this->translator->translator('common_publish_perm'); ?>";
				openMsgDialog(msg);
		<?php } ?>
	});	
	
	$('a.publish_all').click(function() {
		var self = this;
		<?php if ($this->allow('publish','backendsetting','Comment')) { ?>
			var bigrel = $(self).attr('rel');
			if($('input.check_btn:checked').length > 0)
			{
				var id_str = '';
				for(var i=0;i < $('input.check_btn:checked').length;i++)
				{
					if(i == 0)
					{
						 id_str += $('input.check_btn:checked:eq('+i+')').val();
					}
					else
					{
						id_str += ','+$('input.check_btn:checked:eq('+i+')').val();
					}
				}
				
				$.ajax({
						url: "<?php echo $this->url( array('module'=> 'Comment', 'controller' => 'backendsetting', 'action'     => 'publishall' ), 'adminrout',    true) ?>",
						type: 'POST',
						data: { id_st: id_str, paction: bigrel},
						beforeSend: function(){
							$(self).html('<span class="icon loader"></span>');
						 },
						success: function(response) 
						{
							//alert(response);
							var json_arr = eval("("+response+")");
							if(json_arr.status == 'ok')
							{
								if(bigrel=='publish')
								{
									for(var i=0;i < $('input.check_btn:checked').length;i++)
									{								
										var rel = $('input.check_btn:checked:eq('+i+')').parents("td:first").parents("tr:first").children("td:nth-child(7)").children("a:nth-child(1)").attr('rel');
										var rel_arr = rel.split('_');
										var new_rel = rel_arr[0]+'_unpublish';
										$('input.check_btn:checked:eq('+i+')').parents("td:first").parents("tr:first").children("td:nth-child(7)").children("a:nth-child(1)").attr('rel',new_rel);
										$('input.check_btn:checked:eq('+i+')').parents("td:first").parents("tr:first").children("td:nth-child(7)").children("a:nth-child(1)").html('<img src="application/modules/Administrator/layouts/scripts/images/tools/published.gif" border="0" title="<?php echo $this->translator->translator("comment_setting_off_language_title"); ?>" alt="<?php echo $this->translator->translator("comment_setting_off_language_title"); ?>" />');								
									}
								}
								else
								{
									for(var i=0;i < $('input.check_btn:checked').length;i++)
									{								
										var rel = $('input.check_btn:checked:eq('+i+')').parents("td:first").parents("tr:first").children("td:nth-child(7)").children("a:nth-child(1)").attr('rel');
										var rel_arr = rel.split('_');
										var new_rel = rel_arr[0]+'_publish';
										$('input.check_btn:checked:eq('+i+')').parents("td:first").parents("tr:first").children("td:nth-child(7)").children("a:nth-child(1)").attr('rel',new_rel);
										$('input.check_btn:checked:eq('+i+')').parents("td:first").parents("tr:first").children("td:nth-child(7)").children("a:nth-child(1)").html('<img src="application/modules/Administrator/layouts/scripts/images/tools/unpublished.gif" border="0" title="<?php echo $this->translator->translator("comment_setting_on_language_title"); ?>" alt="<?php echo $this->translator->translator("comment_setting_on_language_title"); ?>" />');								
									}	
								}
							}
							else
							{
								openMsgDialog(json_arr.msg);
							}	
							if(bigrel=='publish')
							{	
								$(self).html('<span class="icon publish"><?php echo $this->translator->translator("comment_setting_on_language_title_all"); ?></span>');
							}
							else
							{
								$(self).html('<span class="icon unpublish"><?php echo $this->translator->translator("comment_setting_off_language_title_all"); ?></span>');
							}
						}
				});			
			}
			else
			{
				var msg = "<?php echo $this->translator->translator('comment_setting_selected_err'); ?>";
				openMsgDialog(msg);
			}
		<?php }else{ ?>
			var msg = "<?php echo $this->translator->translator('common_publish_perm'); ?>";
				openMsgDialog(msg);
		<?php } ?>
	});
	
	$('input.checkAll_btn').click(function() {
		var self = this;
		if($(self).is(':checked'))
		{		
			$('input.check_btn').attr('checked', true);
		}
		else
		{
			$('input.check_btn').attr('checked', false);
		}
	});
	
});
</script>

<div id="container">
<?php if (!$this->allow()) : ?>
	<div class="grid_12 t_a_bottom">
		<?php echo $this->translator->translator('Member_Access_deny_desc'); ?>
	</div>
<?php else : ?>

<div class="title-container">
	<div class="comments-setting"><h1><?php echo $this->translator->translator('comment_setting_list_page_name'); ?></h1></div>
    
    <div class="button-container">
		<?php if ($this->allow('publish','backendcomment','Comment')) : ?>
        <span class="button  ui-widget-content-1"><a href="javascript: void(0);" rel="publish" class="publish_all"><span class="icon publish"><?php echo $this->translator->translator('comment_setting_on_language_title_all'); ?></span></a></span>
        <?php endif; ?>
		<?php if ($this->allow('publish','backendcomment','Comment')) : ?>
        <span class="button  ui-widget-content-1"><a href="javascript: void(0);" rel="unpublish" class="publish_all"><span class="icon unpublish"><?php echo $this->translator->translator('comment_setting_off_language_title_all'); ?></span></a></span>
        <?php endif; ?>
    </div>
</div>
<div id="button_top_bar"></div>
<div class="content-container">
	<div id="sidebar-inner">
    	<?php echo $this->partial('backendcomment/nav_menu.phtml');  ?>
    </div>
   
    
    
    <div id="content-inner">
    	<div class="pagination">
        	<?php echo $this->paginationControl($this->datas, 'Sliding', 'top_pagination.phtml'); ?>
        </div>
        
        <div class="chart">
			<div class="data-table">
            	<table id="sortTable" width="100%" align="center" cellpadding="0" cellspacing="0">
                	<thead>
                		<tr>
                            <th width="3%" class="center"><input type="checkbox" name="checkAll" id="checkAll" class="checkAll_btn" /></th>
                            <th width="5%"><?php echo $this->translator->translator('comment_setting_list_page_id'); ?></th>
                            <th width="16%"><?php echo $this->translator->translator('comment_setting_list_page_module'); ?> </th>
                            <th width="16%"><?php echo $this->translator->translator('comment_setting_list_who_view'); ?></th>
                            <th width="23%"><?php echo $this->translator->translator('comment_setting_list_who_comment'); ?></th>
                			<th width="16%"><?php echo $this->translator->translator('comment_setting_list_publish_on_add'); ?></th>
                            <th width="14%"><?php echo $this->translator->translator('comment_setting_list_status'); ?></th>
                            <th width="7%"><?php echo $this->translator->translator('common_action'); ?></th>
                		</tr>
               		</thead>
                    <tbody>
                    <?php 
                    $mem_db = new Members_Model_DbTable_MemberList();
                    $j = 1;
                    $parentInfo = array();
                    foreach ($this->datas as $entry): 
                        if($j % 2 == 0)
                        {
                            $onmouseout = "this.bgColor='#FFFFFF';";
                            $background = '#FFFFFF';
                        }
                        else
                        {
                            $onmouseout = "this.bgColor='#F7F7F7';";
                            $background = '#F7F7F7';
                        }
                    ?>
                    	<tr bgcolor="<?php echo $background; ?>" onmouseover="this.bgColor='#FCF5DD';" onmouseout="<?php echo $onmouseout; ?>">
                      		<td class="center checkbox"><input type="checkbox" name="checkbtn" id="checkbtn" value="<?php echo $this->escape($entry->id); ?>" class="check_btn" /></td>
                      		<td class="center"><?php echo $this->escape($entry->id); ?></td>
                      		<td><?php 	
                                $moduleName = explode('_',$this->escape($entry->module));
                                echo ucfirst($moduleName[0]);
                      			?>
							</td>
                      		<td>
								<?php 
                                	echo ($this->escape($entry->who_view) == 1) ? $this->translator->translator("comment_setting_by_login_title") : $this->translator->translator("comment_setting_everybody_title");
                              	?>
							</td>
                      		<td>
								<?php 
                            		echo ($this->escape($entry->who_comment) == 1) ? $this->translator->translator("comment_setting_by_login_title") : $this->translator->translator("comment_setting_everybody_title");
                          		?>
                          	</td>
                      		<td class="center">
								<?php 
                            		echo ($this->escape($entry->publish_on_add) == 1) ? $this->translator->translator("comment_setting_list_publish_on") : $this->translator->translator("comment_setting_list_publish_off");
                          		?>
                         	</td>
                      		<td class="center"><a href="javascript:void(0);" class="publish_btn" rel="<?php echo $this->escape($entry->id); ?>_<?php if($this->escape($entry->active) == '1'){ ?>unpublish<?php }else{ ?>publish<?php } ?>"><?php if($this->escape($entry->active) == '1'){ ?><img src="application/modules/Administrator/layouts/scripts/images/tools/published.gif" border="0" title="<?php echo $this->translator->translator('comment_setting_off_language_title'); ?>" alt="<?php echo $this->translator->translator('comment_setting_off_language_title'); ?>" /><?php }else{ ?><img src="application/modules/Administrator/layouts/scripts/images/tools/unpublished.gif" border="0" title="<?php echo $this->translator->translator('comment_setting_on_language_title'); ?>" alt="<?php echo $this->translator->translator('comment_setting_on_language_title'); ?>" /><?php } ?></a></td>
                      
                      		<td class="tools-list">
                            	<ul>
                                	<?php if ($this->allow('edit','backendsetting','Comment')) : ?>
                                	<li><a href="<?php echo $this->url( array('module'=> 'Comment', 'controller' => 'backendsetting', 'action'     => 'edit', 'id' => $this->escape($entry->id)), 'adminrout',    true) ?>"><img src="application/modules/Administrator/layouts/scripts/images/tools/edit.png" title="<?php echo $this->translator->translator('common_edit_title'); ?>" border="0" /></a></li>
                                	<?php endif; ?>
								</ul>													  
							</td>
                    	</tr>
                    <?php 													
                        $j++;
                        endforeach;
                   	?>
                   </tbody>
				</table>	
            </div>
		</div>
        
        <div class="pagination">
        	<?php echo $this->paginationControl($this->datas, 'Sliding', 'pagination.phtml'); ?>
        </div>
	</div>
</div>

<div id="floatingbar">
	<h1><?php echo $this->translator->translator('comment_setting_list_page_name'); ?></h1>
    
    <div class="button-container">
		<?php if ($this->allow('publish','backendcomment','Comment')) : ?>
        <span class="button  ui-widget-content-1"><a href="javascript: void(0);" rel="publish" class="publish_all"><span class="icon publish"><?php echo $this->translator->translator('comment_setting_on_language_title_all'); ?></span></a></span>
        <?php endif; ?>
		<?php if ($this->allow('publish','backendcomment','Comment')) : ?>
        <span class="button  ui-widget-content-1"><a href="javascript: void(0);" rel="unpublish" class="publish_all"><span class="icon publish"><?php echo $this->translator->translator('comment_setting_off_language_title_all'); ?></span></a></span>
        <?php endif; ?>
    </div>
</div>
<?php endif; ?>
<div id="dialog_container" title="<?php echo $this->translator->translator('common_dialog_msg',$this->translator->translator('comment_list_page_name')); ?>"></div>
<div id="dialog-confirm" title="<?php echo $this->translator->translator('common_confirmation',$this->translator->translator('common_delete')); ?>"></div>
</div>